package com.web;

import com.DisplayChatters;
import com.NicknameValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@Controller
@RequestMapping("/start")
public class StartController {

    @Autowired
    private NicknameValidation nicknameValidation;
    @Autowired
    private DisplayChatters displayChatters;

    @RequestMapping("/start")
    public String start(HttpServletRequest request, HttpServletResponse response) {


        try {
            if (nicknameValidation.validate(request.getParameter("login"))) {
                if (request.getParameter("login") != null) {
                    request.getSession().setAttribute("nickname", request.getParameter("login"));
                    return "chat";
                }
            }
        } catch (SQLException | NamingException | IOException e1) {
            e1.printStackTrace();
        }

        try {
            request.getSession().setAttribute("users", displayChatters.getUsers());
        } catch (ClassNotFoundException | IOException | SQLException | NamingException e) {
            e.printStackTrace();
        }
        return "start";
    }
}
